﻿using System;
using System.Data.SqlServerCe;
using System.IO;
using System.Reflection;
using Loki.Data;
using Loki.Utils;
using Luna.Data.SqlCe.Messages;

namespace Luna.Data.SqlCe
{
    public class SqlCeDatabaseManager : LokiObject, IDatabaseManager
    {
        private const string NEW_FILE_NAME = "new.sdf";
        private const string DEFAULT_NAMESPACE = "Luna.Data.SqlCe.";

        public IDatabaseAdapter Current
        {
            get;
            set;
        }

        public void Create(string P_DatabaseName)
        {
            FileStream L_Stream = null;
            try
            {
                L_Stream = File.Create(P_DatabaseName);
                Stream L_File = Assembly.GetExecutingAssembly().GetManifestResourceStream(DEFAULT_NAMESPACE + NEW_FILE_NAME);
                L_File.CopyTo(L_Stream);
                L_Stream.Close();
                L_File.Close();
            }
            finally
            {
                if (L_Stream != null)
                {
                    L_Stream.Dispose();
                }
            }

            InitializeConnection(P_DatabaseName);
        }

        public void Open(string P_DatabaseName)
        {
            if (File.Exists(P_DatabaseName))
            {
                try
                {
                    InitializeConnection(P_DatabaseName);
                }
                catch (Exception L_Ex)
                {
                    throw BuildErrorFormat<LokiException>(Errors.SqlCeDatabaseManager_ConnectionError, L_Ex, P_DatabaseName);
                }
            }
            else
            {
                throw BuildErrorFormat<LokiException>(Errors.SqlCeDatabaseManager_FileNotFound, P_DatabaseName);
            }
        }

        public void SaveAs(string P_DatabaseName)
        {
            SqlCeDatabaseAdapter L_Adapter = Current as SqlCeDatabaseAdapter;
            if (L_Adapter != null && !string.IsNullOrEmpty(L_Adapter.FileName))
            {
                try
                {
                    if (File.Exists(P_DatabaseName))
                    {
                        File.Delete(P_DatabaseName);
                    }

                    File.Copy(L_Adapter.FileName, P_DatabaseName);
                    InitializeConnection(P_DatabaseName);
                }
                catch (Exception L_Ex)
                {
                    throw BuildErrorFormat<LokiException>(Errors.SqlCeDatabaseManager_ConnectionError, L_Ex, P_DatabaseName);
                }
            }
            else
            {
                throw BuildError<LokiException>(Errors.SqlCeDatabaseManager_NoCurrentDatabase);
            }
        }

        private void InitializeConnection(string P_DatabaseName)
        {
            if (Current == null)
            {
                throw BuildError<LokiException>(Errors.SqlCeDatabaseManager_NoCurrentDatabase);
            }

            SqlCeConnectionStringBuilder L_Builder = new SqlCeConnectionStringBuilder();
            L_Builder.DataSource = P_DatabaseName;
            L_Builder.PersistSecurityInfo = false;
            Current.ConnectionString = L_Builder.ConnectionString;

            Log.InfoFormat(LogStrings.SqlCeDatabaseManager_Connect, Current.DataSourceDescription);
        }
    }
}
